package org.example.student_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.student_system.aop.anno;
import org.example.student_system.entity.student;
import org.example.student_system.mapper.studentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin(origins = "*")
@Transactional
@Tag(name = "学生信息管理")
public class studentController {
    @Autowired
    private studentMapper studentMapper;

    @anno
    @GetMapping("/list")
    @Operation(summary = "分页查询")
    public IPage<student> list(Integer pageNum, Integer pageSize) {
        log.info("分页查询，页码：{} 每页条数：{}", pageNum, pageSize);
        IPage<student> page=new Page<>(pageNum, pageSize);
        studentMapper.selectPage(page, null);
        return page;
    }

    @anno
    @GetMapping("/get")
    @Operation(summary = "根据姓名查询学生信息")
    public List<student> get(String name) {
        log.info("查询姓名为{}的学生信息", name);
        QueryWrapper<student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return studentMapper.selectList(queryWrapper);
    }

    @anno
    @PostMapping("/add")
    @Operation(summary = "添加学生信息")
    public void add(@RequestBody student student) {
        log.info("添加学生信息{}", student);
        studentMapper.insert(student);
    }

    @anno
    @PostMapping("/update")
    @Operation(summary = "修改学生信息")
    public void update(@RequestBody student student) {
        log.info("修改学生信息{}", student);
        studentMapper.updateById(student);
    }

    @anno
    @DeleteMapping("/delete")
    @Operation(summary = "删除学生信息")
    public void delete(int id) {
        log.info("删除id为{}的学生信息", id);
        studentMapper.deleteById(id);
    }
}
